<?php
/**
 *
 *
 * @author Zeon <scplay@gmail.com>
 * @date 2019/5/28 14:36
 * @copyright PanxSoft Inc.
 */

namespace App\Http\Controllers\App\V1;


use App\Exceptions\ApiAbstractException;
use App\Http\Controllers\ApiController;
use App\Http\Requests\App\V1\UserDevicePreset\UserDevicePresetStoreRequest;
use App\Http\Requests\App\V1\UserDevicePreset\UserDevicePresetUpdateRequest;
use App\Models\UserDevice;
use App\Models\UserDevicePreset;
use App\Services\Utils\Guard;

class UserDevicePresetController extends ApiController
{
    const ROUTES = [
        'index'   => 'app.v1.UserDevicePreset.index',
        'store'   => 'app.v1.UserDevicePreset.store',
        'update'  => 'app.v1.UserDevicePreset.update',
        'destroy' => 'app.v1.UserDevicePreset.destroy',
    ];

    /**
     * 设备预设列表
     *
     * @return array|\Illuminate\Http\Response
     */
    public function index()
    {
        $user_device_presets = Guard::user()->userDevicePresets()->oldest('is_default')->get();

        return $this->response(compact('user_device_presets'));
    }

    /**
     * 新增用户设备预设
     *
     * @param UserDevicePresetStoreRequest $request
     *
     * @return array|\Illuminate\Http\Response
     * @throws ApiAbstractException
     */
    public function store(UserDevicePresetStoreRequest $request)
    {
        $this->checkItemExist(
            $this->isDeviceIdBelongsGuardUser($request->get('user_device_id')),
            trans('checker.app.user_device')
        );

        $store_data = $request->only([
            'name', 'is_default', 'user_device_id',
            'color_temperature', 'luminance'
        ]);
        $store_data['preset'] = $request->presetJSON();

        $user_device_preset = Guard::user()
            ->userDevicePresets()
            ->create($store_data);

        return $this->response(compact('user_device_preset'));
    }

    /**
     * 更新用户设备预设信息
     *
     * @param UserDevicePresetUpdateRequest $request
     *
     * @param $primary_key
     *
     * @return array|\Illuminate\Http\Response
     * @throws ApiAbstractException
     */
    public function update(UserDevicePresetUpdateRequest $request, $primary_key)
    {
        /** @var UserDevicePreset $user_device_preset */
        $user_device_preset = Guard::user()
            ->userDevicePresets()
            ->whereKey($primary_key)
            ->first();
            $this->checkItemExist($user_device_preset, trans('checker.app.user_device_preset'));

        $update_data = array_filter($request->only([
            'name'
        ]));
        $update_data['preset'] = $request->presetJSON();

        $updated = $user_device_preset->update($update_data);

        return $this->response(compact('user_device_preset', 'updated'));
    }

    /**
     * @param $primary_key
     *
     * @return array|\Illuminate\Http\Response
     * @throws \Exception
     */
    public function destroy($primary_key)
    {
        /** @var UserDevicePreset $user_device */
        $user_device_preset = Guard::user()
            ->userDevicePresets()
            ->whereKey($primary_key)
            ->first();
        $this->checkItemExist($user_device_preset, trans('checker.app.user_device_preset'));

        $deleted = $user_device_preset->delete();

        return $this->response(compact('user_device_preset', 'deleted'));
    }

    /**
     * @param int $user_device_id
     *
     * @return bool
     */
    private function isDeviceIdBelongsGuardUser(int $user_device_id): bool
    {
        return Guard::user()
            ->userDevices()
            ->pluck((new UserDevice)->getKeyName())
            ->contains($user_device_id);
    }
}